﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cn.Edwon.Biz;
using Cn.Edwon.Data;
using System.Collections;
using NHibernate.Criterion;
using NHibernate;
using System.Reflection;
using log4net;
using System.Data;
using Spring.Data.Core;
using Spring.Data.Objects;
using Spring.Data.Common;

namespace Cn.Edwon.Spring {
    public class BizSupport : IBiz {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        #region 为spring提供注入的接口
        private DaoSupport _commonDao;
        public DaoSupport commonDao {
            set { _commonDao = value; }
            get { return _commonDao; }
        }
        private AdoDaoSupport _commonAdoDao;
        public AdoDaoSupport commonAdoDao {
            set { _commonAdoDao = value; }
            get { return _commonAdoDao; }
        }

        #endregion

        #region ADO
        public void ADOExecuteNoQuerySql(String rSql) {
            _commonAdoDao.AdoTemplate.ExecuteNonQuery(CommandType.Text,rSql);
        }

        public DataSet ADOExecuteSql(String rSql) {
            return _commonAdoDao.AdoTemplate.DataSetCreate(CommandType.Text, rSql);
        }
        public DataSet ADOExcuteProc(String rProcedureName, IList<CallingParameter> rParams) {
            //build parameters
            String paramStr = "";
            for(int i=0;i<rParams.Count;i++){
                CallingParameter cp = rParams[i];
                if (i > 0)
                    paramStr += ",";
                paramStr += (cp.IsNumber) ? cp.Value : "'" + cp.Value + "'";
            }
            String callStr = "call " + rProcedureName + "(" + paramStr + ")";
            log.Debug("Calling Procedure:" + callStr);
            return _commonAdoDao.AdoTemplate.DataSetCreate(CommandType.Text, callStr);
        }

        #endregion
        #region Spring.Net StoredProcedure
        public IDictionary ExcuteProc(String rProcedureName,params object[] rParams) {
            CommonStoredProc csp = new CommonStoredProc(_commonAdoDao.DbProvider, rProcedureName);
            return csp.Query(rParams);
        }
        public IDictionary ExcuteProcByNamedPara(String rProcedureName, IDictionary rParams) {
            CommonStoredProc csp = new CommonStoredProc(_commonAdoDao.DbProvider, rProcedureName);
            return csp.QueryByNamedParam(rParams);
        }
        #endregion

        #region IBiz 成员

        public Object Save<T>(T eo) {
            return _commonDao.Save<T>(eo);
        }

        public Object Save(String entityName, Object eo) {
            return _commonDao.Save(entityName, eo);
        }

        public void Update(String entityName, Object eo) {
            _commonDao.Update(entityName, eo);
        }

        public void Update<T>(T eo) {
            _commonDao.Update<T>(eo);
        }

        public void Delete(Type entityType, Object eo) {
            _commonDao.Delete(entityType, eo);
        }

        public void Delete(Type entityType, IList keys) {
            _commonDao.Delete(entityType, keys);
        }
        public void Delete<T>(T eo) {
            _commonDao.Delete<T>(eo);
        }
        public void Delete<T>(Object key) {
            T eo = _commonDao.Load<T>(key);
            _commonDao.Delete<T>(eo);
        }

        public void Delete(string queryString) {
            log.Info(queryString);
            _commonDao.Delete(queryString);
        }

        public IList<Object> LoadAll(Type entityType) {
            return _commonDao.LoadAll(entityType);
        }
        public IList<Object> Load(Type entityType, String pk, Int32 page, Int32 pageSize, IList<ICriterion> findCondition, IList<Order> orderCondition) {
            return _commonDao.Load(entityType, pk, page, pageSize, findCondition, orderCondition);
        }
        public IList<T> LoadAll<T>() {
            return _commonDao.LoadAll<T>();
        }

        public T Load<T>(object key) {
            return _commonDao.Load<T>(key);
        }

        public void Load(object eo, object key) {
            _commonDao.Load(eo, key);
        }

        public IList<T> Find<T>(string queryString) {
            log.Info(queryString);
            return _commonDao.Find<T>(queryString);
        }
        public IList<Object> Find(string queryString, int top) {
            log.Info(queryString+",top:"+top);
            return _commonDao.Find(queryString, top);
        }

        public IList<T> Find<T>(string queryString, object[] values) {
            log.Info(queryString);
            return _commonDao.Find<T>(queryString, values);
        }

        public int ExecuteUpdate(String rHql) {
            log.Info(rHql);
            return _commonDao.ExecuteUpdate(rHql);
        }

        public IList<Object> Search(String entityName, IList<SearchCase> fcList) {
            return _commonDao.Search(entityName, fcList);
        }

        public T Load<T>(object key, object ownerKey) {
            T obj = _commonDao.Load<T>(key);
            if (obj.GetType().GetProperty("OwnerKey").GetValue(obj, null).Equals(ownerKey))
                return obj;
            else
                throw new Exception("invalid owner key");
        }



        public IList<Object> LoadAll(Type entityType,String rOwnerKey) {
            return _commonDao.LoadAll(entityType, rOwnerKey);
        }

        public IList<Object> ExcuteSqlQuery(String rSql) {
            log.Info(rSql);
            return _commonDao.ExcuteSqlQuery(rSql);
        }
        public void ExcuteSqlUpdate(String rSql) {
            log.Info(rSql);
            _commonDao.ExcuteSqlUpdate(rSql);
        }
        #endregion

    }
}
